﻿using ChineseSchool.Models.Hethong;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace ChineseSchool.BLL
{
    public class B_Dowork
    {
        private PetaPoco.Database db;
        private B_Lessons lessonService = new B_Lessons();

        public B_Dowork()
        {
            db = new PetaPoco.Database("ConnectionString");
        }

        public M_Dowork GetById(int id)
        {
            return db.FirstOrDefault<M_Dowork>("SELECT * FROM Dowork WHERE DoworkId = " + id);
        }

        public List<M_Lessons> GetLessonsLearning(int userId)
        {
            var lst1 = db.Fetch<M_Dowork>("SELECT * FROM Dowork WHERE UserId = " + userId);

            var xxx = db.Query<DataTable>("");

            var lst2 = new List<M_Lessons>();
            foreach (var lesson in lst1)
            {
                var objLesson = lessonService.GetByPK(lesson.LessonId);
                if (objLesson != null)
                {
                    if (lst2.Any(item => item.LessonID == objLesson.LessonID))
                        continue;
                    lst2.Add(objLesson);
                }
            }
            return lst2;
        }

        public DataTable GetDataStatistic(int userId)
        {
            var strSql = @"SELECT DoworkId, LessonTitle, FileAttach
                          FROM tiengtrungminha.Lessons
                          INNER JOIN tiengtrungminha.Dowork
                          ON tiengtrungminha.Lessons.LessonID = tiengtrungminha.Dowork.LessonId
                          WHERE tiengtrungminha.Dowork.UserId = " + userId;

            var xxx = db.ExecuteReader(strSql);
            return xxx;
        }
    }
}